package com.myebills.ebpsfrontend.cust.maxis.ms01;

import com.myebills.ebpsfrontend.cust.controller.*;
import com.myebills.ebpsfrontend.cust.utils.*;
import com.checkfree.isolutions.customer.persist.*;
import com.checkfree.isolutions.admin.ObjectId;
import com.checkfree.isolutions.statement.persist.Statement;
import com.checkfree.isolutions.utility.CFiError;
import java.util.*;
import java.text.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CusStmtAcctServlet_01_v4r00 extends EbppxTransactionServlet {

    public void doTransaction(HttpServletRequest req, HttpServletResponse res)
    throws ServletException {
        try {
            final HttpSession session = req.getSession(false);
            if (null == session) {
                throw new ServletException("Session must be already established");
            }

    		final String randomKey = (String) session.getAttribute(EbppxConstants.RANDOM_KEY);

            final EbppxUrlManager urlManager = getUrlManager(req);

            final String stmtIndex = urlManager.getSecureParameter("stmtIndex");
            if (stmtIndex.length() == 0) {
                throw new ServletException("Missing statement index");
            }

            final int stmtIdx = Integer.parseInt(EncryptionHelper.decrypt(stmtIndex,randomKey));

            final String sOidAccount = urlManager.getSecureParameter(EbppxConstants.ACCOUNT_ID);
            if (sOidAccount.length() == 0) {
                throw new ServletException("Missing account identifier");
            }

            final Account cfiAccount = getAccountHome().findById(new ObjectId(EncryptionHelper.decrypt(sOidAccount,randomKey)));
            final Vector cfiStatements = StatementUtil.getStatements(cfiAccount,EbppxCusConstants.STMT_START_MONTH);
            final Statement cfiStatement = StatementUtil.getStatement(cfiAccount,EbppxCusConstants.STMT_START_MONTH,stmtIdx);
            String stmtIndexPrev = "-1";
            if ((stmtIdx+1) < cfiStatements.size()) {
                stmtIndexPrev = String.valueOf(stmtIdx+1);
            }

            // update first view
            cfiStatement.view();

            //update last view
            Date date = new Date();
            cfiStatement.setProperty("dateLastViewed",date.toString()); //EEE MMM dd hh:mm:ss z yyyy

            req.setAttribute(EbppxConstants.CFI_ACCOUNT, cfiAccount);
            req.setAttribute(EbppxConstants.CFI_STATEMENT, cfiStatement);
            req.setAttribute("stmtIndex", EncryptionHelper.decrypt(stmtIndex,randomKey));
            req.setAttribute("stmtIndexPrev", stmtIndexPrev);

            final EbppxEcsUser ecsUser = (EbppxEcsUser) session.getAttribute(EbppxConstants.ECS_USER);
            req.setAttribute("userId",ecsUser.getUserId());


            String strAccNo = urlManager.getRequestParameter("accNo");
            //strAccNo = EncryptionHelper.decrypt(strAccNo,randomKey);

            //get the vector of Accounts
            final Vector cfiAccounts = ecsUser.getAccounts();
            final Vector cfiAcctExtKeys = ecsUser.getAccountExternalKeys();
            Vector statements = new Vector();

//poon test start
            final Vector cfiOldAccounts = ecsUser.getOldAccounts();
            req.setAttribute("oldAccounts",cfiOldAccounts);

			//to get old acc no for selected new acc no
			String oldAccountNo = ecsUser.getOldAccount(cfiAcctExtKeys, cfiOldAccounts, strAccNo);
//poon test end

			//to check is there any old account number - start
			int oldAccountDisplayCount = 0;
			String oldAccountDisplay = "N";
			for (java.util.Enumeration enumAccounts = cfiAcctExtKeys.elements(); enumAccounts.hasMoreElements();) {
				final String tAccountNo = (String) enumAccounts.nextElement();

                Merchant cfiMerchant = MerchantHomeImpl.singleton().findByName(ecsUser.getMerchantName());
                Account account = AccountHomeImpl.singleton().findByExternalKeyAndMerchant(tAccountNo, cfiMerchant);
                com.checkfree.isolutions.statement.persist.Statement statement =  StatementUtil.getStatement(account,EbppxCusConstants.STMT_START_MONTH,0);
                statements.addElement(statement);

                req.setAttribute(EbppxConstants.CFI_STATEMENTS, statements);

				/*if (!AcctDetails.getAcctKey(tAccountNo).equals("")) {
					oldAccountDisplayCount++;
				}*/

			}
			/*if (oldAccountDisplayCount > 0) {
				oldAccountDisplay = "Y";
			}*/
            req.setAttribute("oldAccountDisplay",oldAccountDisplay);
			//to check is there any old account number - end

            req.setAttribute(EbppxConstants.CFI_ACCOUNTS, cfiAccounts);
            req.setAttribute("acctExtKeys",cfiAcctExtKeys);
            req.setAttribute("accNo", strAccNo);


            session.setAttribute("fromPage","1");

            // log activity start
            EbppxCusDBUtil.logUserActivity(EbppxConstants.CUS_VIEW,cfiAccount.getExternalKey(),ecsUser.getUserId(),EbppxConstants.CUSTOMER,EbppxConstants.STATUS_VIEWED,DateUtil.formatDate(new Date()),DateUtil.formatDate(cfiStatement.getDate()),EbppxConstants.FROM_STMTACCT,"","","");
            // log activity end

            forwardToMappedUrl(req, res, EbppxConstants.URL_JSP_PARAM);
        } catch (ServletException e) {
            // Just re-throw these
            throw e;
        } catch (Throwable t) {
            throw new ServletException(t.toString(), t);
        }
    }

}